package io.zhengsh.nio.filelock;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.StandardCharsets;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class FileLockDemo2 {


    public static void main(String[] args) throws IOException {
        String input = "hello!";
        System.out.println(input);


        ByteBuffer buffer = ByteBuffer.wrap(input.getBytes(StandardCharsets.UTF_8));
        String pathName = "/Users/zhengsh/sourcecode.io/zhengsh-vvip/nio/src/main/resources/01.txt";
        Path path = Paths.get(pathName);

        FileChannel fileChannel = FileChannel.open(path,
                StandardOpenOption.WRITE, StandardOpenOption.APPEND);
        fileChannel.position(fileChannel.size() - 1);

        // 加锁
        FileLock lock = fileChannel.lock();

        // 如果是共享锁则不能执行后续的 write 操作
        //FileLock lock = fileChannel.lock(0, Long.MAX_VALUE, true);

        System.out.println("isShared: " + lock.isShared());

        fileChannel.write(buffer);
        fileChannel.close();

        readFile(pathName);
    }

    private static void readFile(String pathName) throws IOException {
        FileReader fileReader = new FileReader(pathName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String str = bufferedReader.readLine();
        System.out.println("读取内容：");
        while (str != null) {
            System.out.println("  " + str);
            str = bufferedReader.readLine();
        }
        fileReader.close();
        bufferedReader.close();
    }

}
